package com.hkts.naturalgas.organizational.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.organizational.entity.HontPost;
import com.hkts.naturalgas.organizational.entity.HontUserPost;
import com.hkts.naturalgas.organizational.mapper.HontUserPostMapper;
import com.hkts.naturalgas.organizational.service.IHontUserPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.performance.performanceVo.HontPostTitleVo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2022-09-20
 */
@Service
@Transactional
public class HontUserPostServiceImpl extends ServiceImpl<HontUserPostMapper, HontUserPost> implements IHontUserPostService {


    @Autowired
    private HontUserPostMapper hontUserPostMapper;

    @Override
    public IPage<HontUserPost> selectPage(Page<HontUserPost> page, HontUserPost hontUserPost) {
        QueryWrapper<HontUserPost> query = Wrappers.query();
        QueryWrapper<HontUserPost> entityWrapper = getEntityWrapper(query, hontUserPost);
        return hontUserPostMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HontUserPost> selectList(HontUserPost hontUserPost) {
        QueryWrapper<HontUserPost> query = Wrappers.query();
        QueryWrapper<HontUserPost> entityWrapper = getEntityWrapper(query, hontUserPost);
        return hontUserPostMapper.selectList(entityWrapper);
    }

    @Override
    public List<HontPostTitleVo> getByUserId(String userId) {
        List<HontPostTitleVo> byUserId = hontUserPostMapper.getByUserId(userId);
        return byUserId;
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HontUserPost> getEntityWrapper(QueryWrapper<HontUserPost> entityWrapper, HontUserPost hontUserPost) {
        //条件拼接
        if (StringUtils.isNotBlank(hontUserPost.getCreateBy())) {
            entityWrapper.like(HontUserPost.CREATE_BY, hontUserPost.getCreateBy());
        }
        if (StringUtils.isNotBlank(hontUserPost.getUpdateBy())) {
            entityWrapper.like(HontUserPost.UPDATE_BY, hontUserPost.getUpdateBy());
        }
        if (StringUtils.isNotBlank(hontUserPost.getHontPostId())) {
            entityWrapper.like(HontUserPost.HONT_POST_ID, hontUserPost.getHontPostId());
        }
        if (StringUtils.isNotBlank(hontUserPost.getHontUserId())) {
            entityWrapper.like(HontUserPost.HONT_USER_ID, hontUserPost.getHontUserId());
        }
        return entityWrapper;
    }
}
